package cn.jtfadmin.core.modules.sys.controller;



import cn.jtfadmin.base.dict.dto.DictItemModelDTO;
import cn.jtfadmin.base.dict.service.DictEntityService;
import cn.jtfadmin.base.lang.common.domain.dto.*;
import cn.jtfadmin.base.lang.common.service.BaseService;
import cn.jtfadmin.base.lang.config.excel.ExcelWeb;
import cn.jtfadmin.core.modules.range.DepartRangeRule;
import cn.jtfadmin.core.modules.sys.domain.dto.*;
import cn.jtfadmin.core.modules.sys.domain.entity.SysRoleEntity;
import cn.jtfadmin.core.modules.sys.service.SysRolePermissionActionService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.List;


/**
 * 后管-系统角色管理
 *
 * @author jtf
 * @since 1.0
 */
@RestController
@AllArgsConstructor
public class SysRoleController {


    private BaseService<SysRoleEntity> service;

    private DictEntityService<SysRoleEntity> dictEntityService;

    private DepartRangeRule departRangeRule;


    private SysRolePermissionActionService sysRolePermissionActionService;

    /**
     * 分页查询 系统角色
     *
     * @param specPagePDTO 查询条件
     * @return 分页数据
     */
    @GetMapping(value = "/sys/role/list")
    public ResultDTO<PageRDTO<SysRoleModelDTO>> list(SpecPagePDTO<SysRoleEntity> specPagePDTO) {
        return ResultDTO.successData(service.findModelAll(specPagePDTO,SysRoleModelDTO.class));
    }

    /**
     * 字典
     *
     * @param columns 命名列
     * @return 无
     */
    @GetMapping(value = "/sys/role/named")
    public ResultDTO<List<DictItemModelDTO>> named(@RequestParam(required = false) String columns) {
        return ResultDTO.successData(dictEntityService.findDictModelList(columns));
    }

    /**
     * 新增 系统角色
     *
     * @param addPDTO 新增参数
     * @return id
     */
    @PostMapping(value = "/sys/role/add")
    public ResultDTO<String> add(@RequestBody @Validated SysRoleAddPDTO addPDTO) {
        return ResultDTO.successData(service.add(addPDTO).getId());
    }

    /**
     * 编辑 系统角色
     *
     * @param editPDTO 查询条件
     * @return 无
     */
    @PutMapping(value = "/sys/role/edit")
    public ResultDTO<String> edit(@RequestBody @Validated SysRoleEditPDTO editPDTO) {
        return ResultDTO.successData(service.edit(editPDTO).getId());
    }

    /**
     * 按照id删除 系统角色
     *
     * @param id id
     * @return 无
     */
    @DeleteMapping(value = "/sys/role/del")
    public ResultDTO<String> del(@RequestParam() @NotBlank String id) {
        service.deleteById(id);
        return ResultDTO.success();
    }

    /**
     * 批量删除 系统角色
     *
     * @param ids 查询条件
     * @return 分页数据
     */
    @DeleteMapping(value = "/sys/role/delBatch")
    public ResultDTO<String> delBatch(@RequestParam() @NotBlank String ids) {
        service.deleteBatch(ids);
        return ResultDTO.success();
    }

    /**
     * 部门
     *
     * @return
     */
    @GetMapping(value = "/sys/role/roleRange")
    public ResultDTO<List<AntdvTreeDataRDTO>> roleRange() {
        return ResultDTO.successData(departRangeRule.getAntdTreeDataRDTO());
    }

    /**
     * 修改角色权限
     *
     * @param sysRolePermissionEditPDTO 修改参数
     * @return 结果
     */
    @PostMapping(value = "/sys/role/saveRolePermission")
    public ResultDTO<String> savePermissions(@RequestBody @Validated SysRolePermissionEditPDTO sysRolePermissionEditPDTO) {
        return sysRolePermissionActionService.savePermissions(sysRolePermissionEditPDTO);
    }

    /**
     * 删除角色用户
     *
     * @param roleId 角色id
     * @param userId 用户id
     * @return 结果
     */
    @DeleteMapping(value = "/sys/role/deleteUser")
    public ResultDTO<String> deleteUser(@RequestParam @NotBlank String roleId,
                                        @NotBlank @RequestParam(value = "id") String userId) {
        return deleteUserBatch(roleId, userId);
    }


    /**
     * 删除角色用户
     *
     * @param roleId  角色id
     * @param userIds 用户ids
     * @return 结果
     */
    @DeleteMapping(value = "/sys/role/deleteUserBatch")
    public ResultDTO<String> deleteUserBatch(@RequestParam @NotBlank String roleId,
                                             @NotBlank @RequestParam(value = "ids") String userIds) {
        return sysRolePermissionActionService.deleteUserBatch(roleId, userIds);
    }

    /**
     * 授权角色用户
     *
     * @param sysRoleUserGrantPDTO 授权参数
     * @return 结果
     */
    @PostMapping(value = "/sys/role/grantUser")
    public ResultDTO<String> grantUser(@RequestBody @Validated SysRoleUserGrantPDTO sysRoleUserGrantPDTO) {
        return sysRolePermissionActionService.grantUser(sysRoleUserGrantPDTO);
    }

    /**
     * 导入excel
     */
    @PostMapping("/sys/role/importExcel")
    public ResultDTO<String> importExcel(MultipartFile file) {
        try {
            service.importExcel(file.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return ResultDTO.success();
    }

    /**
     * 导出excel
     */
    @GetMapping("/sys/role/exportExcel")
    public void exportExcel(SpecPDTO<SysRoleEntity> specPDTO) {
        service.exportExcel(specPDTO, ExcelWeb.addCurrentResposeHeader("系统角色"));
    }
}
